package com.vodafone.mapreduce.config.beans;

import org.apache.commons.lang.StringUtils;



/**
 * JDBC access configuration to target database
 * 
 * @author $Author: rainer.bieniek $
 * @since 1.0-SNAPSHOT
 * @version $Id$
 */
public class DatabaseConfiguration implements IDbConfiguration {
	
	private String uri;
	private String login;
	private String password;
	private String hibernateDialect;
	private String driverClassName;
	private EDatabaseType databaseType;
	private int jdbcFetchSize = 32768;
	private int jpaFetchSize = 32768;
	
	/**
	 * construct the configuration holder bean
	 * 
	 * @param uri JBDC URI
	 * @param login JDBC login
	 * @param password JDBC passwpord
	 * @param type database type. Supported types are "oracle", "mysql" and "innoDB"
	 */
	public DatabaseConfiguration(String uri, String login, String password, String type) {
		this.uri = uri;
		this.login = login;
		this.password = password;
		
		if(StringUtils.isBlank(type) || StringUtils.equals(type, "oracle")) {
			hibernateDialect = "org.hibernate.dialect.Oracle9Dialect";
			this.driverClassName = "oracle.jdbc.OracleDriver";
			this.databaseType = EDatabaseType.ORACLE;
		} else if(StringUtils.equals(type, "mysql")) {
			hibernateDialect = "org.hibernate.dialect.MySQL5Dialect";
			this.driverClassName = "com.mysql.jdbc.Driver";
			this.databaseType = EDatabaseType.MYSQL;
		} else if(StringUtils.equals(type, "innoDB")) {
			hibernateDialect = "org.hibernate.dialect.MySQL5InnoDBDialect";
			this.driverClassName = "com.mysql.jdbc.Driver";
			this.databaseType = EDatabaseType.MYSQL;
		} else if(StringUtils.equals(type, "postgres")) {
			hibernateDialect = "org.hibernate.dialect.PostgreSQLDialect";
			this.driverClassName = "org.postgresql.Driver";
			this.databaseType = EDatabaseType.POSTGRESQL;			
		} else if(StringUtils.equals(type, "derby")) {
			this.driverClassName = "org.apache.derby.jdbc.EmbeddedDriver";
			hibernateDialect = /*"org.hibernate.dialect.DerbyDialect"*/ "com.vodafone.next.usage.reporting.support.hibernate.dialect.LargeClobDerbyDialect";
			this.databaseType = EDatabaseType.DERBY;                       
		}
	}

	/* (non-Javadoc)
	 * @see com.vodafone.next.usage.reporting.configuration.beans.IDbConfiguration#getUri()
	 */
	public String getUri() {
		return uri;
	}

	/* (non-Javadoc)
	 * @see com.vodafone.next.usage.reporting.configuration.beans.IDbConfiguration#getLogin()
	 */
	public String getLogin() {
		return login;
	}

	/* (non-Javadoc)
	 * @see com.vodafone.next.usage.reporting.configuration.beans.IDbConfiguration#getPassword()
	 */
	public String getPassword() {
		return password;
	}

	/* (non-Javadoc)
	 * @see com.vodafone.next.usage.reporting.configuration.beans.IDbConfiguration#getHibernateDialect()
	 */
	public String getHibernateDialect() {
		return hibernateDialect;
	}

	@Override
	public String getDriverClassName() {
		return this.driverClassName;
	}

	@Override
	public EDatabaseType getDatabaseType() {
		return this.databaseType;
	}

	/**
	 * @return the jdbcFetchSize
	 */
	public int getJdbcFetchSize() {
		return jdbcFetchSize;
	}

	/**
	 * @param jdbcFetchSize the jdbcFetchSize to set
	 */
	public void setJdbcFetchSize(int jdbcFetchSize) {
		this.jdbcFetchSize = jdbcFetchSize;
	}

	/**
	 * @return the jpaFetchSize
	 */
	public int getJpaFetchSize() {
		return jpaFetchSize;
	}

	/**
	 * @param jpaFetchSize the jpaFetchSize to set
	 */
	public void setJpaFetchSize(int jpaFetchSize) {
		this.jpaFetchSize = jpaFetchSize;
	}

}
